package main

import (
	"github.com/prometheus/client_golang/prometheus"
)

var (
	valueResultMetric = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "zabbix_value_result",
			Help: "Metrics from Zabbix ValueResult",
		},
		[]string{"hostid", "itemid", "name", "key", "snmp_oid"},
	)
)

type ZabbixExporter struct {
	zabbixURL string
	token     string
}

func NewZabbixExporter(zabbixURL, token string) *ZabbixExporter {
	return &ZabbixExporter{
		zabbixURL: zabbixURL,
		token:     token,
	}
}

// Implement Describe method
func (e *ZabbixExporter) Describe(ch chan<- *prometheus.Desc) {
	valueResultMetric.Describe(ch)
}

// Implement Collect method
func (e *ZabbixExporter) Collect(ch chan<- prometheus.Metric) {
	// 获取 hostids
	hostids := getHosts(e.zabbixURL, e.token)

	// 获取 value results
	valueResults := getValues(e.zabbixURL, e.token, hostids)

	// 更新 metrics
	for _, vr := range valueResults {
		valueResultMetric.With(prometheus.Labels{
			"hostid":   vr.HostID,
			"itemid":   vr.ItemID,
			"name":     vr.Name,
			"key":      vr.Key,
			"snmp_oid": vr.SnmpOID,
		}).Set(float64(len(vr.LastValue))) // 这里只是一个示例，根据实际情况设置值
	}

	// 将采集到的 metrics 发送到 Prometheus
	valueResultMetric.Collect(ch)
}